﻿using Common;
using DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManagerSys
{
    /// <summary>
    /// 判断项目或者任务的开始时间到结束时间是否包含有节日或者补班
    /// </summary>
    public class TimeValidate
    {
        /// <summary>
        /// 取出开始时间到结束时间的假期或者补班的天数
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="datetype">类型 1：假期 2：补班</param>
        /// <returns></returns>
        public int GetDays(DateTime startTime, DateTime endTime,int datetype)
        {
            int days = 0;
            if (!string.IsNullOrEmpty(startTime.ToString()) && !string.IsNullOrEmpty(endTime.ToString()))
            {
                //取出开始时间和结束时间的月份和年份的假期
                using (var con = Data.DB.GetConnection())
                {
                    List<MANAGER_HOLIDAY> list = new List<MANAGER_HOLIDAY>();
                    if (datetype == 1)
                        list = con.MANAGER_HOLIDAY.Where(c => c.HOLIDAY >= startTime && c.HOLIDAY <= endTime && c.DATETYPE == datetype && c.ACTIVE == 1 && c.ISWORKDAY == 0).ToList();
                    else if (datetype == 2)
                        list = con.MANAGER_HOLIDAY.Where(c => c.HOLIDAY >= startTime && c.HOLIDAY <= endTime && c.DATETYPE == datetype && c.ACTIVE == 1).ToList();
                    days = list.Count;
                }
            }
            
            return days;
        }

        /// <summary>
        /// 检查日期是否是节假日或者是不是周末，true 是 false 不是 
        /// </summary>
        /// <param name="searchTime">查询日期</param>
        /// <returns></returns>
        public bool isHoliday(DateTime searchTime)
        {
            searchTime = searchTime.Date;
            using (var con = Data.DB.GetConnection())
            {
                //检查是否是节假日
                var oModel = con.MANAGER_HOLIDAY.Where(c=>c.HOLIDAY == searchTime && c.DATETYPE==1).FirstOrDefault();
                if (oModel != null) return true;
                //检查是否是周末
                var day = searchTime.DayOfWeek;
                //判断是否为周末
                if (day == DayOfWeek.Sunday || day == DayOfWeek.Saturday)
                {
                    //检查是否是补班
                    var model = con.MANAGER_HOLIDAY.Where(c => c.HOLIDAY == searchTime && c.DATETYPE == 2).FirstOrDefault();
                    if (model != null) return false; //补班
                    return true;
                }                   
            }
            return false;
        }

        /// <summary>
        /// 获取设置好的假期获取补休
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="datetype">类型 1：假期 2：补班</param>
        /// <returns></returns>
        public List<MANAGER_HOLIDAY> GetWeekOrWorkList(DateTime startTime, DateTime endTime,int datetype)
        {
            List<MANAGER_HOLIDAY> list = new List<MANAGER_HOLIDAY>();
            if (!string.IsNullOrEmpty(startTime.ToString()) && !string.IsNullOrEmpty(endTime.ToString()))
            {
                //取出开始时间和结束时间的月份和年份的假期
                using (var con = Data.DB.GetConnection())
                {
                    list = con.MANAGER_HOLIDAY.Where(c => c.HOLIDAY >= startTime && c.HOLIDAY <= endTime && c.DATETYPE == datetype && c.ACTIVE == 1).ToList();
                }
            }

            return list;
        }

    }
}
